//
//  SearchHistoryView.m
//  搜索功能
//
//  Created by Hello Cai on 2020/10/10.
//  Copyright © 2020 Hello Cai. All rights reserved.
//

#import "SearchHistoryView.h"

@interface SearchHistoryView ()
@end

@implementation SearchHistoryView

- (instancetype)initWithFrame:(CGRect)frame textArray:(NSArray *)textArray
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.textArray = [textArray mutableCopy];
        [self setView];
    }
    return self;
}

-(NSMutableArray *)textArray{
    if (!_textArray) {
        _textArray = [NSMutableArray array];
    }
    return _textArray;
}

-(void)setView{
    //模拟读取缓存
//    NSArray *textArray = @[@"东胜球馆", @"东圃", @"趣运动东英大羽毛球", @"三只松鼠", @"土鸡蛋", @"2018最新款女", @"伟盛羽毛球馆", @"男装打折大促销",@"帽子"];
//    [self.textArray addObjectsFromArray:textArray];
    
    if (self.textArray.count == 0) {
        //无搜索记录
        return;
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    
    //设置搜索历史
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 80, 20)];
    titleLab.text = @"最近搜索";
    titleLab.font = [UIFont systemFontOfSize:16];
    [scrollView addSubview:titleLab];
    
    //设置删除按钮
    UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-15-30-12, 0, 15+25, 30)];
    [deleteBtn setImage:[UIImage imageNamed:@"ic_delete拷贝"] forState:UIControlStateNormal];
    deleteBtn.centerY = titleLab.centerY;
    [deleteBtn addTarget:self action:@selector(cleanClick) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:deleteBtn];

    CGFloat btnX = 14;
    CGFloat btnTop = CGRectGetMaxY(titleLab.frame) + 15;
    for (NSInteger i = self.textArray.count - 1; i >= 0; i--) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [scrollView addSubview:btn];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.backgroundColor = RGB(240, 240, 240);
        btn.tag = 100+i;
        [btn setTitle:self.textArray[i] forState:UIControlStateNormal];
        UIFont *font = [UIFont systemFontOfSize:14];
        btn.titleLabel.font = font;
        btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        btn.layer.cornerRadius = 15;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(recentSearchClick:) forControlEvents:UIControlEventTouchUpInside];
        
        NSDictionary *attributes = @{NSFontAttributeName:font};
        
        CGFloat textWidth = [self.textArray[i] boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        CGFloat btnWidth = 20 + textWidth;
        CGFloat btnHeight = 32;
        
        if (btnX + btnWidth > kScreenW-30) {
            btnX = 10;
            btnTop = btnTop + btnHeight + 10;
        }
        btn.frame = CGRectMake(btnX, btnTop, btnWidth, btnHeight);
        btnX = btnX + btnWidth + 10;
    }
}

//更新顺序
-(void)updateOrder{
    if (self.textArray.count > 0) {
        [self.scrollView removeFromSuperview];
        [self setView];
    }
}

//清除
-(void)cleanClick{
    if (self.cleanClickBlock) {
        self.cleanClickBlock();
    }
}

-(void)recentSearchClick:(UIButton *)button{
    NSInteger index = button.tag - 100;
    NSString *text = self.textArray[index];
    NSLog(@"text:%@", text);
    
    if (self.textClickBlock) {
        self.textClickBlock(text);
    }
}
@end
